package com.yw.android.common.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Preference工具类
 *
 * @author yuanwei1 2014.10.24
 */
public class PreferenceUtil {

    private final static String PREFERENCE_NAME = "SySearch";
    private final static PreferenceUtil sInstance = new PreferenceUtil();
    private SharedPreferences mPref;

    public static PreferenceUtil getInstance() {
        return sInstance;
    }

    public synchronized void init(Context mContext) {
        if (mPref == null) {
            mPref = mContext.getApplicationContext().getSharedPreferences(PREFERENCE_NAME, Context.MODE_MULTI_PROCESS);
        }
    }

    public SharedPreferences pref() {
        return mPref;
    }

    public long getLong(String key, long defValue) {
        if (mPref == null) {
            return defValue;
        }
        return mPref.getLong(key, defValue);
    }

    public PreferenceUtil putLong(String key, long value) {
        if (mPref != null) {
            mPref.edit().putLong(key, value).apply();
        }
        return this;
    }

    public int getInt(String key, int defValue) {
        if (mPref == null) {
            return defValue;
        }
        return mPref.getInt(key, defValue);
    }

    public PreferenceUtil putInt(String key, int value) {
        if (mPref != null) {
            mPref.edit().putInt(key, value).apply();
        }
        return this;
    }

    public String getString(String key, String defValue) {
        if (mPref == null) {
            return defValue;
        }
        return mPref.getString(key, defValue);
    }

    public PreferenceUtil putBoolean(String key, boolean value) {
        if (mPref != null) {
            mPref.edit().putBoolean(key, value).apply();
        }
        return this;
    }

    public boolean getBoolean(String key, boolean defValue) {
        if (mPref == null) {
            return defValue;
        }
        return mPref.getBoolean(key, defValue);
    }

    public PreferenceUtil putString(String key, String value) {
        if (key != null && value != null && mPref != null) {
            mPref.edit().putString(key, value).apply();
        }
        return this;
    }

}
